package com.itheima.Service.Impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.Bean.Category;
import com.itheima.Bean.Dish;
import com.itheima.Bean.Setmeal;
import com.itheima.Dao.CategoryDao;
import com.itheima.Service.CategoryService;
import com.itheima.Service.DishService;
import com.itheima.Service.SetmealService;
import com.itheima.exception.CustomException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Slf4j
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryDao, Category> implements CategoryService {

    @Autowired
    private DishService dishService;

    @Autowired
    private SetmealService setmealService;

    @Override
    public void remove(Long id) {
        LambdaQueryWrapper<Dish> dlqw = new LambdaQueryWrapper<>();
        dlqw.eq(Dish::getCategoryId,id);
        int count1 = dishService.count(dlqw);
        if(count1>0){throw new CustomException("当前分类关联了菜品，不能删除！"); }

        LambdaQueryWrapper<Setmeal> slqw = new LambdaQueryWrapper<>();
        slqw.eq(Setmeal::getCategoryId,id);
        int count2 = setmealService.count(slqw);
        if(count2>0){throw new CustomException("当前分类关联了套餐，不能删除！"); }

        super.removeById(id);
    }
}
